<?php
/**************************************
 *
 * @Repositories 数据层~主要与数据库打交道
 *
 **************************************/
namespace app\common\repositories;
use app\common\model\UserModel;
use app\common\model\CalendarModel;
use think\Model;
use think\db;
use app\common\services\RedisServices;

class RankingRepositories
{
    protected $UserModel;
    protected $CalendarModel;
    protected $RedisServices;
    protected $clickRepositories;
    // 修改用户信息自动过滤字段
    protected $field = ['mood', 'img', 'update_time', 'label', 'is_arti'];
    //点赞自动筛选字段
    protected $clickFile = ['click'];

    public function __construct()
    {
        $this->UserModel = new UserModel();
        $this->CalendarModel = new CalendarModel();
        $this->RedisServices = new RedisServices();
        $this->clickRepositories = new ClickRepositories();
    }

    public function regist($data, $request_data)
    {
        $appid = $request_data['appid'];
        $user = $this->UserModel->where('open_id', $data['openid'])->find();
        if (!empty($user)) {
            return $user['id'];
        } else {
            $data['appid'] = $appid;
            $data['open_id'] = $data['openid'];
            $data['nickname'] = $request_data['nickname'];
            $data['img'] = $request_data['img'];
            $data['wx_session'] = $this->generate3rdSession($data['openid']);
            if (isset($data['unionid'])) {
                $data['unionid'];
            }
            unset($data['openid']);
            $data['create_time'] = time();
            $checkData = $this->UserModel->checkData($data);
            if ($checkData != 'success') {
                return $checkData;
            }
            $res = $this->UserModel->allowField(['appid','open_id','session_key','unionid','wx_session','create_time', 'nickname', 'img'])->save($data);
            if (is_null($res)) {
                return false;
            }
            return $this->UserModel->id;
        }
    }

    /**
     * 生成3rd_session
     *
     * @param $openid
     * @return string
     */
    private function generate3rdSession($openid)
    {
        return md5(mt_rand() . $openid);
    }

    //周榜
    public function weekList($param)
    {
        $user_id = null;
        if (isset($param['user_id'])) {
            $user_id = $param['user_id'];
        }
//        if (isset($param['view']) && !empty($param['view']) && $param['view'] != 'null') {
//            $map['view'] = array('elt',$param['view']);
//        }
//        if (isset($param['id'])) {
//            $map['id'] = array('neq',$param['id']);
//        }
        $map['status'] = 1;
        switch($param['sign'])
        {
            case 'week':
                $item = Db::table('article')
                    ->field('id, click, title, desc, img, view')
                    ->where("YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now())")
                    ->where($map)
                    ->order('view desc,id desc')
                    ->limit(20)
                    ->select();
                break;
            case 'mooth':
                $item = Db::table('article')
                    ->field('id, click, title, desc, img, view')
                    ->where("date_format(create_time,'%Y-%m')=date_format(now(),'%Y-%m')")
                    ->where($map)
                    ->order('view desc')
                    ->limit(50)
                    ->select();
                break;
            case 'total':
                $item = Db::table('article')
                    ->field('id, click, title, desc, img, view')
                    ->where($map)
                    ->order('view desc')
                    ->limit(100)
                    ->select();
                break;
        }
        if (!$item) {
            return false;
        }
        //点赞数量
        for ($i=0; $i<count($item);$i++) {
            $data[$i]['id'] = $item[$i]['id'];
            $data[$i]['user_id'] = $user_id;
        }
        $clickNum = $this->clickRepositories->clickNum($data);
        if ($clickNum) {
            for ($i=0; $i<count($item);$i++) {
                $item[$i]['click'] = $clickNum[$i]['click'];
                $item[$i]['whether'] = $clickNum[$i]['whether'];
            }
        }
        //人头
        $newImg = $this->clickRepositories->getClickImg($data);
        for ($i=0; $i<count($item);$i++) {
            $item[$i]['newImg'] = $newImg[$i]['click'];
        }
        //浏览量
        for ($i=0; $i<count($data);$i++) {
            $viewNum = $this->clickRepositories->getView($data[$i]['id']);
            if ($viewNum) {
                $item[$i]['view'] = $viewNum;
            }
        }
        $arr = array_zjSort($item);
        $array = array_reverse($arr);
        return $array;
    }

}